<?php

require_once('persistence/_table_names.php');
require_once('configuration/pdo/AbstractPdoConfig.class.php');

define('_LOCAL_CONFIG_DRIVER', 'sqlite');
define('_LOCAL_CONFIG_DB_FILENAME', 'jsug_database.db');

class LocalSqlitePdoConfig extends AbstractPdoConfig {

	private $createTableStatements;
	
	public function LocalSqlitePdoConfig() {
		$this->createTableStatements = array(
			TBL_USER => 'CREATE TABLE IF NOT EXISTS ' . TBL_USER . ' ( ' .
				'user_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'username VARCHAR(80) NOT NULL, ' .
				'password VARCHAR(80) NOT NULL, ' .
				'email VARCHAR(80) NOT NULL ' .
			')',
			TBL_VOTE => 'CREATE TABLE IF NOT EXISTS ' . TBL_VOTE . ' ( ' .
				'user_id INTEGER NOT NULL, ' .
				'topic_id INTEGER NOT NULL, ' .
				'created TIMESTAMP NOT NULL, ' .
				'PRIMARY KEY (user_id, topic_id),' . 
				'FOREIGN KEY (user_id) REFERENCES ' . TBL_USER . ' (user_id), ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id) ' .
			')',
			
			TBL_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_TOPIC . ' ( ' .
				'topic_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'user_id INTEGER NOT NULL, ' .
				'title VARCHAR(255) NOT NULL, ' .
				'created TIMESTAMP NOT NULL, ' .
			
				'FOREIGN KEY (user_id) REFERENCES ' . TBL_USER . ' (user_id) ' .
			')',
			TBL_OFFERED_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_OFFERED_TOPIC . ' ( ' .
				'offered_topic_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'topic_id INTEGER NOT NULL, ' .
				'offered_category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id), ' .
				'FOREIGN KEY (offered_category_id) REFERENCES ' . TBL_OFFERED_CATEGORY . ' (offered_category_id) ' .
			')',
			TBL_NEEDED_TOPIC => 'CREATE TABLE IF NOT EXISTS ' . TBL_NEEDED_TOPIC . ' ( ' .
				'needed_topic_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'topic_id INTEGER NOT NULL, ' .
				'needed_category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (topic_id) REFERENCES ' . TBL_TOPIC . ' (topic_id), ' .
				'FOREIGN KEY (needed_category_id) REFERENCES ' . TBL_CATEGORY . ' (needed_category_id) ' .
			')',
			
			TBL_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_CATEGORY . ' ( ' .
				'category_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'title VARCHAR(255) NOT NULL' .
			')',
			TBL_OFFERED_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_OFFERED_CATEGORY . ' ( ' .
				'offered_category_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (category_id) REFERENCES ' . TBL_CATEGORY . ' (category_id) ' .
			')',
			TBL_NEEDED_CATEGORY => 'CREATE TABLE IF NOT EXISTS ' . TBL_NEEDED_CATEGORY . ' ( ' .
				'needed_category_id INTEGER PRIMARY KEY AUTOINCREMENT, ' .
				'category_id INTEGER NOT NULL, ' .
				'FOREIGN KEY (category_id) REFERENCES ' . TBL_CATEGORY . ' (category_id) ' .
			')'
		);	
	}
	
	protected function _newPDO() {
		$dns = _LOCAL_CONFIG_DRIVER . ':' . $this->getDataFolder() . _LOCAL_CONFIG_DB_FILENAME;
		Log::instance()->debug("Connecting with dns [$dns].");
		
		$pdo = new PDO($dns);
		if($pdo == null) {
			throw new Exception('Could not connect with dns [' . $dns . ']!');
		}
		return $pdo;
	}
	
	private function getDataFolder() {
		$path = Configuration::getInstance()->getBasePath() . 'data' . DIRECTORY_SEPARATOR;
		/* TODO check permissions on unix systems
		if($folderSeparator == '/') {
			echo substr(sprintf('%o', fileperms($result)), -4);
		}
		*/
		return $path;
	}
	
	public function getCreateStatement($tableName) {
		$res = $this->createTableStatements[$tableName];
		#echo '{'.$res.'}';
		if($res == null) {
			throw new Exception('Invalid table name [' . $tableName . ']!');
		}
		return $res;
	}
	
}

?>